package com.whmall.service.crm.module.companyuser.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import com.whmall.component.util.NumberUtils;
import com.whmall.service.crm.entity.dto.companyuser.CompanyUserQuotationInvervalReqDTO;
import com.whmall.component.entity.dto.CompanyUserQuotationInvervalDTO;
import com.whmall.service.crm.module.companyuser.CompanyUserQuotationInvervalService;
import com.whmall.service.crm.module.companyuser.dao.CompanyUserQuotationInvervalMapper;
import com.whmall.service.crm.module.companyuser.entity.CompanyUserQuotationInvervalDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CompanyUserQuotationInvervalServiceImpl implements CompanyUserQuotationInvervalService {

    @Autowired
    CompanyUserQuotationInvervalMapper quotationInvervalMapper;

    @Override
    public List<CompanyUserQuotationInvervalDTO> listByUserId(Long userId) {

        Assert.notNull(userId, "用户id不能为空");
        List<CompanyUserQuotationInvervalDO> companyUserQuotationInvervalDOS = quotationInvervalMapper
                .selectByUserId(userId);
        return companyUserQuotationInvervalDOS.stream().map(quotationInvervalDO -> {
            CompanyUserQuotationInvervalDTO rspDTO = new CompanyUserQuotationInvervalDTO();
            BeanUtils.copyProperties(quotationInvervalDO, rspDTO);
            return rspDTO;
        }).collect(Collectors.toList());
    }

    @Override
    public Map<Long, List<CompanyUserQuotationInvervalDTO>> queryUserQuotationIntervalMap(List<Long> userIdList) {
        List<CompanyUserQuotationInvervalDO> list = quotationInvervalMapper.selectByUserIds(userIdList);
        if(CollUtil.isEmpty(list)){
            return MapUtil.empty();
        }
        return list.stream().collect(Collectors.groupingBy(CompanyUserQuotationInvervalDO::getUserId,Collectors.mapping(t -> {
            CompanyUserQuotationInvervalDTO rspDTO = new CompanyUserQuotationInvervalDTO();
            BeanUtils.copyProperties(t, rspDTO);
            return rspDTO;
        },Collectors.toList())));
    }

    @Override
    @Transactional
    public boolean saveOrUpdate(List<CompanyUserQuotationInvervalReqDTO> list, Long employeeId) {

        if (CollUtil.isEmpty(list)){
            return true;
        }
        List<CompanyUserQuotationInvervalReqDTO> inserList = new ArrayList<>();
        List<CompanyUserQuotationInvervalReqDTO> updateList = new ArrayList<>();
        List<CompanyUserQuotationInvervalDO> deleteList = new ArrayList<>();
        list.forEach(e -> {
            if (e.getId() == null){
                inserList.add(e);
            }else {
                updateList.add(e);
            }
        });
        List<CompanyUserQuotationInvervalDO> companyUserQuotationInvervalDOS = quotationInvervalMapper
                .selectByUserId(list.get(0).getUserId());
        List<Long> idList = companyUserQuotationInvervalDOS.stream()
                .map(e -> e.getId()).collect(Collectors.toList());
        //批量新增
        if (CollUtil.isNotEmpty(inserList)){

            List<CompanyUserQuotationInvervalDO> userQuotationInvervalDOS = inserList.stream().map(e -> {

                CompanyUserQuotationInvervalDO quotationInvervalDO = new CompanyUserQuotationInvervalDO();
                BeanUtils.copyProperties(e, quotationInvervalDO);
                quotationInvervalDO.setStartPrice(NumberUtils.multiply100(e.getStartPrice()));
                quotationInvervalDO.setEndPrice(NumberUtils.multiply100(e.getEndPrice()));
                quotationInvervalDO.setIsDeleted(0);
                quotationInvervalDO.setCreateId(employeeId);
                quotationInvervalDO.setCreateTime(new Date());
                quotationInvervalDO.setUpdateId(0l);
                quotationInvervalDO.setUpdateTime(DateUtil.parse("1972-01-01 00:00:00"));
                return quotationInvervalDO;
            }).collect(Collectors.toList());
            quotationInvervalMapper.batchInsert(userQuotationInvervalDOS);
        }

        //批量修改
        updateList.forEach(e -> {
            CompanyUserQuotationInvervalDO quotationInvervalDO = new CompanyUserQuotationInvervalDO();
            BeanUtils.copyProperties(e, quotationInvervalDO);
            quotationInvervalDO.setStartPrice(NumberUtils.multiply100(e.getStartPrice()));
            quotationInvervalDO.setEndPrice(NumberUtils.multiply100(e.getEndPrice()));
            quotationInvervalDO.setIsDeleted(0);
            quotationInvervalDO.setUpdateId(employeeId);
            quotationInvervalDO.setUpdateTime(new Date());
            quotationInvervalMapper.updateByIdSelective(quotationInvervalDO);
            if (idList.contains(e.getId())){
                idList.remove(e.getId());
            }
        });
        //批量删除
        if (CollUtil.isNotEmpty(idList)){
            idList.forEach(id -> {

                companyUserQuotationInvervalDOS.forEach(companyUserQuotationInvervalDO -> {
                    if (companyUserQuotationInvervalDO.getId().equals(id)){
                        deleteList.add(companyUserQuotationInvervalDO);
                    }
                });
            });
            deleteList.forEach(companyUserQuotationInvervalDO -> {
                companyUserQuotationInvervalDO.setIsDeleted(1);
                companyUserQuotationInvervalDO.setUpdateId(employeeId);
                companyUserQuotationInvervalDO.setUpdateTime(new Date());
                quotationInvervalMapper.updateByIdSelective(companyUserQuotationInvervalDO);
            });
        }
        return true;
    }
}
